/** @file    Location.cpp
 *  @author  Alessandro Polo
 *  @version $Id: Location.cpp 2821 2010-08-01 11:44:26Z alex $
 *  @brief
 * File containing methods for the wosh::building::Location class.
 * The header for this class can be found in Location.h, check that file
 * for class description.
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/
 
 #include <framework/building/Location.h>

 #include <core/MethodsCommon.h>
 #include <core/MessageFilter.h>
 #include <core/SecurityManager.h>
 #include <core/MessageProcessingCommon.h>

namespace wosh {
 namespace building {

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////// CONSTRUCTORS

Location::Location()
	: Object() {
	Object::setName( "wosh.building.Location", _GET_Abs(Utilities::randomLong()) );
	Object::getURI().setName( Object::getName() );
	Properties.set( _KEY_name, Object::getName() );
	Properties.set( _KEY_type, Location::className(), Permission(Permission::Read) );

	setLocationType(TYPE_UNKNOWN);
	setLocationCategory(CATEGORY_UNKNOWN);

	// register default methods
	Methods.setPermission( Object::getPermission(), Permission::Overwrite_All, true, true );
	Methods.getListPermission().setMask( Permission::Read, Permission::Read, Permission::Read );

	MethodPropertyT::createMethods( &Methods, &Properties, NULL );

//	MethodChT::createMethods( &Methods, this, &Properties, NULL ); // &Log

 }

Location::Location( const Location& m ) : Object(m) {
	//*this = m;
	Properties = m.Properties;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////// SETTERS

void Location::setParentName( const std::string& value ) {
	this->parentName = value;
	//Properties.update();
 }

void Location::setLocationType( LOCATION_TYPE type ) {
	this->locationType = type;
	//Properties.update();
 }

void Location::setLocationCategory( LOCATION_CATEGORY category ) {
	this->locationCategory = category;
	//Properties.update();
 }

void Location::setAccessTo( Location* location, double probability ) {
	if ( location == NULL ) return;
	this->accessTo[location->getName()] = probability;
	location->accessTo[getName()] = probability;
 }
void Location::setAccessTo( const std::string& location, double probability ) {
	this->accessTo[location] = probability;
 }

///////////////////////////////////////////////////////////////////////////////////////////////////// SETTERS
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////// PROPERTY EVENT

bool Location::updatingProperty( bool& , const Variant& value_proposed, Property& property_current, const PropertiesProvider* ) {
	if ( property_current.getKey() == _KEY_name ) { // if we are here, we allow to edit name
		//Log(LOG_VERBOSE, " Changing Location Name, Log-Context will change to '%s'", value_proposed.toString().c_str() );
		Object::setName( value_proposed.toString() );
		//Log.setContext( value_proposed.toString() );
		return true;
	 }
	else if ( property_current.getKey() == _KEY_DebugLevel )
		return false;//MPC_PropertyParse_LogLevel( value_proposed, this->Log );

	else if ( property_current.getKey() == "asd" ) {

	 }
	return true;
 }

////////////////////////////////////////////////////////////////////////////////////////////// PROPERTY EVENT
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

bool Location::updateHash( DataHashContext* hash_context ) const {
	if ( hash_context == NULL ) return false;
	bool ret = Object::updateHash( hash_context );
	ret = hash_context->update( this->parentName ) & ret;
	ret = hash_context->update( this->locationType ) & ret;
	ret = hash_context->update( this->locationCategory ) & ret;
	ret = hash_context->update( this->accessTo ) & ret;

	ret = this->Properties.updateHash( hash_context ) & ret;
	ret = this->Interfaces.updateHash( hash_context ) & ret;
	return ret;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*
bool Location::isUserInLocation( const std::string& username ) const
 {
	if ( this->users.find(username) == this->users.end() )
		return false;
	if ( this->users[username].timestamp <= 0 )
		return false;
	if ( this->users[username].probability == 0.0 )
		return false;
	return true;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

std::vector<std::string> Location::getDevicesURI() const
 {
	std::vector<std::string> tmp;
	std::map<URI, std::string>::const_iterator it;
	std::map<URI, std::string>::const_iterator it_end = this->devices.end();
	for ( it=this->devices.begin(); it!=it_end; ++it ) {
		tmp.push_back(it->first.toString());
	 }
	return tmp;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

std::vector<std::string> Location::getUsersName() const
 {
	std::vector<std::string> tmp;
	std::map<std::string, UserInfoLocationData>::const_iterator it;
	std::map<std::string, UserInfoLocationData>::const_iterator it_end = this->users.end();
	for ( it=this->users.begin(); it!=it_end; ++it ) {
		tmp.push_back(it->first);
	 }
	return tmp;
 }

std::string Location::getUser( unsigned int index ) const
 {
	unsigned int i = 0;
	std::map<std::string, UserInfoLocationData>::const_iterator it;
	std::map<std::string, UserInfoLocationData>::const_iterator it_end = this->users.end();
	for ( it=this->users.begin(); it!=it_end; ++it, ++i ) {
		if ( i == index )
			return it->first;
	 }
	return "";
 }
*/
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

 }; // namespace building
}; // namespace wosh
